﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace FieldDevice.UI
{
    public partial class CurveMapForm : Form
    {
        public CurveMapForm()
        {
            InitializeComponent();
            this.initColor();
            this.Title.Text = Device.DeviceName + "电流、电压曲线图";
            this.Refresh();
        }
        public CurveMapForm(Device device)
        {
            InitializeComponent();
            Device = device;
            this.Title.Text = device.DeviceName+ "电流、电压曲线图";
            this.initColor();
            this.Refresh();
        }

        Device Device;
        Color curA_Color;
        Color curB_Color;
        Color curC_Color;
        Color volA_Color;
        Color volB_Color;
        Color volC_Color;
        Color linevolA_Color;
        Color linevolB_Color;
        Color linevolC_Color;

        float max_Vol_Value = 500;
        float max_Cur_Value = 100;
        private bool flag_realtime = true;
        private bool flag_history = false;

        public List<float> curA = new List<float> { 0 };
        public List<float> curB = new List<float> { 0 };
        public List<float> curC = new List<float> { 0 };
        public List<float> volA = new List<float> { 0 };
        public List<float> volB = new List<float> { 0 };
        public List<float> volC = new List<float> { 0 };
        public List<float> linevolA = new List<float> { 0 };
        public List<float> linevolB = new List<float> { 0 };
        public List<float> linevolC = new List<float> { 0 };
       

        /// <summary>
        ///初始化曲线颜色
        /// </summary>
        private void initColor()
        {
            curA_Color = this.curA_colorbox.BackColor;
            curB_Color = this.curB_colorbox.BackColor;
            curC_Color = this.curC_colorbox.BackColor;
            volA_Color = this.phaseA_colorbox.BackColor;
            volB_Color = this.phaseB_colorbox.BackColor;
            volC_Color = this.phaseC_colorbox.BackColor;
            linevolA_Color = this.lineA_colorpan.BackColor;
            linevolB_Color = this.lineB_colorpan.BackColor;
            linevolC_Color = this.lineC_colorpan.BackColor;
        }

        public void CoordinateValue()
        {
            if (this.Device.Category == Device.DeviceCategory.PowerMeter)
            {
                max_Vol_Value = 500;
                max_Cur_Value = 20;
            }
            if (this.Device.Category == Device.DeviceCategory.HighVoltageDevice||
                this.Device.Category==Device.DeviceCategory.EmaxE3)
            {
                max_Vol_Value = 150000;
                max_Cur_Value = 1000;
            } 
        }

        /// <summary>
        /// 绘制A相电流曲线图
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void graph_Paint_curA(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            graph.DrawingValue(curA, max_Cur_Value, curC_Color, g);
        }

        /// <summary>
        /// 绘制B相电流曲线图
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void graph_Paint_curB(object sender, PaintEventArgs e)
        {
            graph.DrawingValue(curB, max_Cur_Value, curB_Color, e.Graphics);
        }

        /// <summary>
        /// 绘制C相电流曲线图
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void graph_Paint_curC(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            graph.DrawingValue(curC, max_Cur_Value, curC_Color,g);
        }

        /// <summary>
        /// 绘制A相电压曲线图
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void graph_Paint_volA(object sender, PaintEventArgs e)
        {
            graph.DrawingValue(volA, max_Vol_Value, volA_Color, e.Graphics);
        }

        /// <summary>
        /// 绘制B相电压曲线图
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void graph_Paint_volB(object sender, PaintEventArgs e)
        {
            graph.DrawingValue(volB, max_Vol_Value, volB_Color, e.Graphics);
        }

          /// <summary>
          /// 绘制C相电压曲线图
          /// </summary>
          /// <param name="sender"></param>
          /// <param name="e"></param>
        private void graph_Paint_volC(object sender, PaintEventArgs e)
        {
            graph.DrawingValue(volC, max_Vol_Value, volC_Color, e.Graphics);
        }
        public void UpdateReads()
        {
            if (flag_realtime == true && flag_history == false)
            {
                this.curA.Add(this.Device.CurrentReads["IA"]);
                this.curB.Add(this.Device.CurrentReads["IB"]);
                this.curC.Add(this.Device.CurrentReads["IC"]);
                this.volA.Add(this.Device.CurrentReads["VA"]);
                this.volB.Add(this.Device.CurrentReads["VB"]);
                this.volC.Add(this.Device.CurrentReads["VC"]);
                this.linevolA.Add(this.Device.CurrentReads["Vab"]);
                this.linevolB.Add(this.Device.CurrentReads["Vbc"]);
                this.linevolC.Add(this.Device.CurrentReads["Vca"]);
                this.graph.Refresh();
            }
        }
        /// <summary>
        /// 清空读数
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cleardata()
        {
            this.curA.Clear();
            this.curB.Clear();
            this.curC.Clear();
            this.volA.Clear();
            this.volB.Clear();
            this.volC.Clear();
            this.linevolA.Clear();
            this.linevolB.Clear();
            this.linevolC.Clear();
        }
        /// <summary>
        /// 从数据库中查找历史读数
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
         public void DrawHistory()
        {
            Power.DsFieldRead.FieldReadsDataTable table = FieldDevice.Power.PowerStorage.StaticPowerStorage.ReadValues((int)this.Device.NCPAddress);
            DateTime starttime =Convert.ToDateTime(this.dateTimePicker1.Text);
            foreach(Power.DsFieldRead.FieldReadsRow row in table.Rows)
            {
                DateTime checktime = row.ReadTime;
                float val = (float)row.ReadValue;
                string name = row.ReadName;
                if (DateTime.Compare(starttime, checktime) > 0)
                {
                    switch (name)
                    { 
                        case "IA":
                            this.curA.Add(val);
                            break;
                        case "IB":
                            this.curB.Add(val);
                            break;
                        case "IC":
                            this.curC.Add(val);
                            break;
                        case "VA":
                            this.volA.Add(val);
                            break;
                        case "VB":
                            this.volB.Add(val);
                            break;
                        case "VC":
                            this.volC.Add(val);
                            break;
                        case "Vab":
                            this.linevolA.Add(val);
                            break;
                        case "Vbc":
                            this.linevolB.Add(val);
                            break;
                        case "Vca":
                            this.linevolC.Add(val);
                            break;
                        default:
                            break;
                    }                   
                }
            }
        }

        /// <summary>
        /// 画相电流曲线图
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void graph_paint_current(object sender, PaintEventArgs e)
        {
            this.maxVol_label.Text = max_Cur_Value.ToString("F2");
            this.midVol_label.Text = (max_Cur_Value / 2).ToString("F2");
            this.minVol_label.Text = "0.00";
            this.unit_vol.Text = "单位: A";
            
            graph.DrawingValue(curA, max_Cur_Value, curA_Color, e.Graphics);
            graph.DrawingValue(curB, max_Cur_Value, curB_Color, e.Graphics);
            graph.DrawingValue(curC, max_Cur_Value, curC_Color, e.Graphics);
        }

        /// <summary>
        /// 画相电压曲线图
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void graph_paint_phasevol(object sender, PaintEventArgs e)
        {
            this.maxVol_label.Text = max_Vol_Value.ToString("F2");
            this.midVol_label.Text = (max_Vol_Value / 2).ToString("F2");
            this.minVol_label.Text = "0.00";
            this.unit_vol.Text = "单位: V";

            graph.DrawingValue(volA, max_Vol_Value, volA_Color, e.Graphics);
            graph.DrawingValue(volB, max_Vol_Value, volB_Color, e.Graphics);
            graph.DrawingValue(volC, max_Vol_Value, volC_Color, e.Graphics);
        }

        /// <summary>
        /// 画线电压曲线图
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void graph_paint_linevol(object sender, PaintEventArgs e)
        {
            this.maxVol_label.Text = max_Vol_Value.ToString("F2");
            this.midVol_label.Text = (max_Vol_Value / 2).ToString("F2");
            this.minVol_label.Text = "0.00";
            this.unit_vol.Text = "单位: V";

            graph.DrawingValue(linevolA, max_Vol_Value, linevolA_Color, e.Graphics);
            graph.DrawingValue(linevolB, max_Vol_Value, linevolB_Color, e.Graphics);
            graph.DrawingValue(linevolC, max_Vol_Value, linevolC_Color, e.Graphics);
        }

        private void cur_button_Click_1(object sender, EventArgs e)
        {
            graph.Paint += new PaintEventHandler(graph_paint_current);
        }

        private void phasevol_button_Click(object sender, EventArgs e)
        {
            graph.Paint += new PaintEventHandler(graph_paint_phasevol);
        }

        private void linevol_button_Click(object sender, EventArgs e)
        {
            graph.Paint += new PaintEventHandler(graph_paint_linevol);
        }

        private void rt_button_Click(object sender, EventArgs e)
        {
            this.flag_realtime = true;
            this.flag_history = false;
            this.cleardata();
        }

        private void histsearch_button_Click(object sender, EventArgs e)
        {
            this.flag_realtime = false;
            this.flag_history = true;
            this.cleardata();
            this.DrawHistory();
        }



        /// <summary>
        /// 选择A相电流曲线颜色
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void curA_colorbox_Click(object sender, EventArgs e)
        {
            ColorDialog colorpan = new ColorDialog();
            colorpan.ShowDialog();
            this.curA_Color = colorpan.Color;
            this.curA_colorbox.BackColor = colorpan.Color;
        }

        /// <summary>
        /// 选择B相电流曲线颜色
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void curB_colorbox_Click(object sender, EventArgs e)
        {
            ColorDialog colorpan = new ColorDialog();
            colorpan.ShowDialog();
            this.curB_Color = colorpan.Color;
            this.curB_colorbox.BackColor = colorpan.Color;
        }

        /// <summary>
        /// 选择C相电流曲线颜色
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void curC_colorbox_Click(object sender, EventArgs e)
        {
            ColorDialog colorpan = new ColorDialog();
            colorpan.ShowDialog();
            this.curC_Color = colorpan.Color;
            this.curC_colorbox.BackColor = colorpan.Color;
        }
        /// <summary>
        /// 选择A相电压曲线颜色
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void phaseA_colorbox_Click(object sender, EventArgs e)
        {
            ColorDialog colorpan = new ColorDialog();
            colorpan.ShowDialog();
            this.volA_Color = colorpan.Color;
            this.phaseA_colorbox.BackColor = colorpan.Color;
        }

        /// <summary>
        /// 选择B相电压曲线颜色
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void phaseB_colorbox_Click(object sender, EventArgs e)
        {
            ColorDialog colorpan = new ColorDialog();
            colorpan.ShowDialog();
            this.volB_Color = colorpan.Color;
            this.phaseB_colorbox.BackColor = colorpan.Color;
        }

        /// <summary>
        /// 选择C相电压曲线颜色
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void phaseC_colorbox_Click(object sender, EventArgs e)
        {
            ColorDialog colorpan = new ColorDialog();
            colorpan.ShowDialog();
            this.volC_Color = colorpan.Color;
            this.phaseC_colorbox.BackColor = colorpan.Color;
        }

        /// <summary>
        /// AB线电压曲线颜色
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void lineA_colorpan_Click(object sender, EventArgs e)
        {
            ColorDialog colorpan = new ColorDialog();
            colorpan.ShowDialog();
            this.linevolA_Color = colorpan.Color;
            this.lineA_colorpan.BackColor = colorpan.Color;
        }

        /// <summary>
        /// BC线电压曲线颜色
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void lineB_colorpan_Click(object sender, EventArgs e)
        {
            ColorDialog colorpan = new ColorDialog();
            colorpan.ShowDialog();
            this.linevolB_Color = colorpan.Color;
            this.lineB_colorpan.BackColor = colorpan.Color;
        }

        /// <summary>
        /// CA线电压曲线颜色
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void lineC_colorpan_Click(object sender, EventArgs e)
        {
            ColorDialog colorpan = new ColorDialog();
            colorpan.ShowDialog();
            this.linevolC_Color = colorpan.Color;
            this.lineC_colorpan.BackColor = colorpan.Color;
        }

    }

}
